/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.IndexSettingRegion;
import com.bifrost.entity.MerchantStore;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/10/20 10:53
 */
public interface IndexSettingRegionRepository extends JpaRepository<IndexSettingRegion, Long>,JpaSpecificationExecutor<IndexSettingRegion>{
    @Override
    List<IndexSettingRegion> findAll();

    List<IndexSettingRegion> findByIsVisibleOrderBySortOrderAsc(Boolean visible);
    List<IndexSettingRegion> findByIsVisibleAndMerchantStoreOrderBySortOrderAsc(Boolean visible, MerchantStore store);

    /**
     * 查询门店配置信息
     * @param store
     * @param indexRegionIds
     * @return
     */
    @Query(value = "SELECT i FROM  IndexSettingRegion  i WHERE i.merchantStore=?1 AND i.indexRegionId IN(?2)")
    List<IndexSettingRegion> findAllByMerchantStoreAndIndexRegionId(MerchantStore store,Collection indexRegionIds);

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Modifying
    @Query( value = "DELETE FROM IndexSettingRegion i WHERE i.indexRegionId IN(?1) AND i.merchantStore=?2")
    void deleteByIndexRegionIdAAndMerchantStore(Collection ids,MerchantStore store);
}
